package com.rayboo.example.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Stream API 示例
 *
 * @author ray
 * @since 2019/07/29
 *
 * <p>Stream API，它是java 8的另一个新功能。该API的所有类和接口都在java.util.stream包中。
 * 通过使用流，我们可以对从集合，数组，输入/输出操作返回的数据执行各种聚合操作。
 *
 * <p>https://beginnersbook.com/2017/10/java-8-stream-tutorial/
 */
public class StreamExample {

    public static void main(String[] args) {
        List<String> strings = new ArrayList<String>();
        strings.add("Ajeet");
        strings.add("Negan");
        strings.add("Aditya");
        strings.add("Steve");

        //Using Stream and Lambda expression
        long count = strings.stream().filter(str -> str.length() < 6).count();
        System.out.println("There are " + count + " strings with length less than 6");


        Stream.iterate(1, num -> num + 1)
                .filter(number -> number % 3 == 0)
                .limit(6)
                .forEach(System.out::print);
        System.out.println();

        //list 1
        List<String> alphabets = Arrays.asList("A", "B", "C");
        //list 2
        List<String> names = Arrays.asList("Sansa", "Jon", "Arya");

        //creating two streams from the two lists and concatenating them into one
        Stream<String> opStream = Stream.concat(alphabets.stream(), names.stream());

        //displaying the elements of the concatenated stream
        opStream.forEach(str -> System.out.print(str + " "));
        System.out.println();
    }
}
